import React, { PureComponent } from 'react'

import axios from 'axios'

import request from './service/request'

export default class App extends PureComponent {
  constructor(props) {
    super(props)

    this.state = {
      products: []
    }
  }

  componentDidMount() {
    // this.setState({
    //   products: [...this.state.products, ...res]
    // })

    // axios({
    //   url: 'http://rap2api.taobao.org/app/mock/273029/learn_axios',
    //   params: {
    //     name: 'l',
    //     age: 18
    //   }
    // }).then(res => {
    //   console.log(res)
    // }).catch(err => {
    //   console.log(err)
    // })

    const request1 = axios({
      url: '/learn_axios',
      params: {
        name: 'l',
        age: 18
      }
    })
    const request2 = axios({
      url: '/learn_axios',
      params: {
        name: 'l',
        age: 18
      }
    })

    // axios.all([request1, request2]).then(res => {
    //   console.log(res)
    // }).catch(err => {

    // })

    const promise1 = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('promise1')
      }, 10)
    })
    const promise2 = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('promise2')
      }, 3000)
    })

    // Promise.all([promise1, promise2]).then(res => {
    //   console.log(res)
    // })

    // const instance1 = axios.create({
    //   baseURL: 'http://rap2api.taobao.org/app/mock/273029/',
    //   timeout: 5000,
    //   headers: {

    //   }
    // })

    // const instance2 = axios.create({
    //   baseURL: 'http://rap2api.taobao.org/app/mock/273029/',
    //   timeout: 10000,
    //   headers: {

    //   }
    // })
    axios.interceptors.request.use(config => {
      // 1.发送网络请求时显示 loading组件
      // 2.要求用户请求必须携带token 跳转至登录页面
      // 3.params/data序列化操作
      return config
    }, err => {

    })
    axios.interceptors.response.use(res => {
      return res.data
    }, err => {
      if(err && err.response) {
        switch(err.response.status) {
          case 400:
            console.log('请求错误')
            break
          case 401:
            console.log('请求错误')
            break
          default:
            console.log('err')
        }
      }
      return err
    })

    // axios.get('http://rap2api.taobao.org/app/mock/273029/learn_axios', {
    //   params:{
    //     name: '11ll'
    //   }
    // }).then(res => {
    //   console.log(res)
    // }).catch(err => {
    //   console.log(err)
    // })

    request.get('/learn_axios', {
      params: {
        name: '1'
      }
    }).then(res => {
      console.log(res)
    }).catch(err => {

    })

  }

  render() {
    return (
      <div>
        App
      </div>
    )
  }
}